package com.example.cunion.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.cunion.common.R;
import com.example.cunion.config.shiro.JwtUtil;
import com.example.cunion.controller.form.ChatForm;
import com.example.cunion.controller.form.PageFom;
import com.example.cunion.controller.form.SearchInfoHistoryForm;
import com.example.cunion.entity.Chat;
import com.example.cunion.entity.User;
import com.example.cunion.service.ChatService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Resource
    private ChatService chatService;

    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/chat")
    public R chat(@RequestHeader("token") String token, @RequestBody ChatForm form){
        String userId = jwtUtil.getUserId(token);
        form.setSenderId(userId);
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Integer result = chatService.sendInfo((HashMap<String, Object>) map);
        return R.ok();
    }

    @PostMapping("/searchInfoHistory")
    public R searchInfoHistory(@RequestHeader("token") String token, @RequestBody SearchInfoHistoryForm form){
        String userId = jwtUtil.getUserId(token);
        form.setReceiverId(userId);
        form.setSenderId("88888888");
        Map<String, Object> map = BeanUtil.beanToMap(form);
        List<HashMap> list = chatService.searchInfoHistory((HashMap) map);
        return R.ok().put("result", list);
    }

    @GetMapping("/searchMyInfo")
    public R searchMyInfo(@RequestHeader("token") String token, @RequestParam("senderId") String senderId){
        String userId = jwtUtil.getUserId(token);
        List<HashMap> list = chatService.searchMyMessage(userId, senderId);
        return R.ok().put("result", list);
    }

    @GetMapping("/searchPrivateChatObject")
    public R searchPrivateChatObject(@RequestHeader("token") String token){
        String userId = jwtUtil.getUserId(token);
        List<User> users = chatService.searchPrivateChatObject(userId);
        return R.ok().put("result", users);
    }


    @PostMapping("/addChatByMp")
    public R addChatByMp(@RequestHeader("token") String token, @RequestBody Chat chat) {
        chatService.addChat(chat);
        return R.ok();
    }

    @GetMapping("/deleteChatByMp")
    public R deleteChatByMp(@RequestHeader("token") String token, @RequestParam("chatId") String chatId) {
        chatService.deletedChat(chatId);
        return R.ok();
    }
    @PostMapping("/updateChatByMp")
    public R updateChatByMp(@RequestHeader("token") String token, @RequestBody Chat chat) {
        chatService.updateChat(chat);
        return R.ok();
    }
    @PostMapping("/searchChatByMp")
    public R searchChatByMp(@RequestHeader("token") String token, @RequestBody PageFom form){
        List<Chat> list = chatService.searchChat(form.getStart(), form.getLength());
        return R.ok().put("result", list);
    }
}
